<?php

namespace catchAdmin\phone\model;

use catcher\exceptions\AlertErrorException;

class SiXin extends \catchAdmin\model\SiXin
{

    public function saveData($param)
    {
        $this->saveDataCC([
            'from_user_id' => $this->getLoginId(),
            'to_user_id'   => $param['to_user_id'],
            'content'      => $param['content'],
        ]);

        return 1;
    }


    ######################################## 显示 ################################################

    public function index($param)
    {
        $app = $this
            ->where('to_user_id', $this->getLoginId())
            ->with([
                'fromUser',
            ])
            ->order('id', 'desc');

        $list = $this->indexDataCC($param, $app);

        $list['no_read'] = (new \catchAdmin\model\SiXin())
            ->where('to_user_id', $this->getLoginId())
            ->where('read_time', 0)
            ->count() ?? 0;

        return $list;
    }


    public function readData($param)
    {
        $msg = $this
            ->where('id', $param['id'])
            ->where('to_user_id', $this->getLoginId())
            ->with([
                'fromUser',
            ])
            ->find();
        if (empty($msg)) {
            throw new AlertErrorException("数据不存在");
        }

        $msg->read_time = time();
        $msg->save();

        return $msg;
    }

}
